﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Threading;
using Microsoft.Xna.Framework.Input;
using LGE;

namespace UpUp
{
    public class ScoreScreen : GameScreen
    {
        PlayScreen ps;

        bool isAnim = true;

        int animDuration = 2000;
        double animTimer = 0;

        float a = 0;

        // sprites
        Texture2D deadPlayer;
        Texture2D diamante;
        Texture2D mascara;
        Texture2D montanha_borda;
        Texture2D montanha_preenchimento;
        Texture2D record;
        Texture2D touca;
        Texture2D you_died;

        // positions
        float ydPosYMax;
        Vector2 ydPos;
        Vector2 mascaraPos = new Vector2(590, 97);
        Vector2 montanha_bordaPos = new Vector2(700, 124);
        Vector2 montanha_preenchimentoPos = new Vector2(700, 124);//new Vector2(702, 138);
        Vector2 diamantePos = new Vector2(848, 604);
        Vector2 nDiamantesPos = new Vector2(842, 608); // superior direito

        Vector2 toucaPosTarget;
        Vector2 toucaPos = new Vector2(631, 567);
        Vector2 toucaPosInicial = new Vector2(631, 567);
        Vector2 toucaPosFinal = new Vector2(782, 110);

        Vector2 nAltitudePosTarget;
        Vector2 nAltitudePos = new Vector2(584, 600);
        Vector2 nAltitudePosInicial = new Vector2(584, 600);
        Vector2 nAltitudePosFinal = new Vector2(735, 143);

        Vector2 recordPosTarget;
        Vector2 recordPos = new Vector2(692, 574);
        Vector2 recordPosInicial = new Vector2(692, 574);
        Vector2 recordPosFinal = new Vector2(692, 124);

        Vector2 nRecordPosTarget;
        Vector2 nRecordPos = new Vector2(1008, 589);
        Vector2 nRecordPosInicial = new Vector2(1008, 589);
        Vector2 nRecordPosFinal = new Vector2(1008, 139);

        // aux
        float ydSpeed = 0.5f;
        float ydAcc = 0.5f;
        float hPercentage = 0;
        float animPercentage = 0;
        Rectangle srPreench;
        int y = 0; // y target do sourceRectangle do preenchimento da montanha

        // Audio
        const String SFX_Folder = "Audio\\SFX\\";
        String SFX_Score = SFX_Folder + "Score";

        public ScoreScreen(PlayScreen ps)
            : base()
        {
            this.ps = ps;
            ydPos = new Vector2(259 + ps.c.drawPosition.X - ps.c.normalDrawPosition.X, -100);
            ydPosYMax = ps.c.drawPosition.Y - 66;
        }

        public override void LoadContent()
        {
            deadPlayer = Engine.Load<Texture2D>("Images\\Character\\dead");
            diamante = Engine.Load<Texture2D>("Images\\ScoreScreen\\diamante");
            mascara = Engine.Load<Texture2D>("Images\\ScoreScreen\\mascara");
            montanha_borda = Engine.Load<Texture2D>("Images\\ScoreScreen\\montanha_borda");
            montanha_preenchimento = Engine.Load<Texture2D>("Images\\ScoreScreen\\montanha_preenchimento");
            record = Engine.Load<Texture2D>("Images\\ScoreScreen\\record");
            touca = Engine.Load<Texture2D>("Images\\ScoreScreen\\touca");
            you_died = Engine.Load<Texture2D>("Images\\ScoreScreen\\you_died");
            Audio.AddSFX(SFX_Score);
            int Hb = ps.levelHeight * 5;

            hPercentage = (float)ps.HMaxInThisSession / Hb;
            toucaPosTarget = toucaPosInicial + hPercentage * (toucaPosFinal - toucaPosInicial);
            nAltitudePosTarget = nAltitudePosInicial + hPercentage * (nAltitudePosFinal - nAltitudePosInicial);

            float recordPercentage = (float)ps.c.maxH / Hb;
            recordPosTarget = recordPosInicial + recordPercentage * (recordPosFinal - recordPosInicial);
            nRecordPosTarget = nRecordPosInicial + recordPercentage * (nRecordPosFinal - nRecordPosInicial);

            y = (int)(hPercentage * montanha_preenchimento.Height);
        }

        public override void Update(GameTime gameTime)
        {
            if (Input.KeyPressed(Keys.Enter) || Input.KeyPressed(Keys.Space) || Input.ButtonPressed(Buttons.A))
            {
                ps.c.Load();
                ps.bm.Load();
                UpUp.isMouseVisible = true;
                ps.isMarket = true;
                ps.isLive = false;
                GameScreenManager.Pop();
                Audio.PlaySong("Audio\\BGM\\Market");
                return;
            }

            ps.UpdateBackgroundAndSnow(gameTime);
            if (isAnim)
            {
                animTimer += gameTime.ElapsedGameTime.TotalMilliseconds;
                if (animTimer > animDuration)
                {
                    animTimer = 0;
                    a = 1;
                    isAnim = false;
                    return;
                }
                ydPos.Y += (ydSpeed += ydAcc);
                if (ydPos.Y > ydPosYMax) ydPos.Y = ydPosYMax;
                a = (float)(animTimer / animDuration);
                if (a > 1) a = 1;
            }
            else
            {
                if (animTimer > animDuration)
                {
                    animPercentage = 1;
                }
                else
                {
                    Audio.PlaySFX(SFX_Score);
                    animTimer += gameTime.ElapsedGameTime.TotalMilliseconds;
                    animPercentage = (float)(animTimer / animDuration);
                    if (animPercentage > 1) animPercentage = 1;
                }
            }

            toucaPos = toucaPosInicial + animPercentage * (toucaPosTarget - toucaPosInicial);
            nAltitudePos = nAltitudePosInicial + animPercentage * (nAltitudePosTarget - nAltitudePosInicial);
            recordPos = recordPosInicial + animPercentage * (recordPosTarget - recordPosInicial);
            nRecordPos = nRecordPosInicial + animPercentage * (nRecordPosTarget - nRecordPosInicial);
            srPreench = new Rectangle(0, montanha_preenchimento.Height - (int)(y * animPercentage), montanha_preenchimento.Width, (int)(y * animPercentage));
            montanha_preenchimentoPos.Y = montanha_bordaPos.Y + montanha_preenchimento.Height - (y * animPercentage);
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            ps.Draw(gameTime, spriteBatch);
            Color transColor = new Color(0f, 0f, 0f, a);
            spriteBatch.Draw(GameScreenManager.blackScreen, Vector2.Zero, transColor);

            if (!isAnim) spriteBatch.Draw(record, recordPos, Color.White);
            if (!isAnim) spriteBatch.Draw(mascara, mascaraPos, transColor);
            
            ps.snowFlakesManagerBack.Draw(gameTime, spriteBatch);
            //ps.snowFlakesManagerFront.Draw(gameTime, spriteBatch);

            spriteBatch.Draw(deadPlayer, ps.c.drawPosition, null, Color.White, 0, new Vector2(deadPlayer.Width / 2, deadPlayer.Height / 2), 1, ps.c.isFacingLeft ? SpriteEffects.FlipHorizontally : SpriteEffects.None, 0);
            spriteBatch.Draw(you_died, ydPos, Color.White);
            if (isAnim) return;

            spriteBatch.DrawString(UpUp.Font, (int)(ps.c.maxH * animPercentage) + "m", nRecordPos, new Color(255, 219, 49));
            spriteBatch.Draw(montanha_borda, montanha_bordaPos, Color.White);
            spriteBatch.Draw(montanha_preenchimento, montanha_preenchimentoPos, srPreench, Color.White);
            spriteBatch.Draw(diamante, diamantePos, Color.White);
            String text = "+" + ps.crystalsInThisSession;
            spriteBatch.DrawString(UpUp.Font, text, nDiamantesPos, new Color(255, 219, 49), 0, new Vector2(UpUp.Font.MeasureString(text).X, 0), 1, SpriteEffects.None, 0);
            spriteBatch.Draw(touca, toucaPos, Color.White);
            spriteBatch.DrawString(UpUp.Font, (int)(ps.HMaxInThisSession * animPercentage) + "m", nAltitudePos, new Color(255, 219, 49));
        }
    }
}
